﻿@model Kooboo.CMS.Sites.Models.SiteMapNode
@if (Model != null)
{
    var allowToPublish = ServiceFactory.UserManager.Authorize(Kooboo.CMS.Sites.Models.Site.Current, ViewContext.HttpContext.User.Identity.Name, Kooboo.CMS.Account.Models.Permission.Sites_Page_PublishPermission);
    var isLocal = Model.Page.IsLocalized(Kooboo.CMS.Sites.Models.Site.Current);
    var isStaticPage = Kooboo.CMS.Sites.Services.ServiceFactory.PageManager.IsStaticPage(Kooboo.CMS.Sites.Models.Site.Current, Model.Page);

    var hasEditPagePermission = Kooboo.CMS.Sites.Services.ServiceFactory.UserManager.Authorize(Kooboo.CMS.Sites.Models.Site.Current,
        Html.ViewContext.HttpContext.User.Identity.Name, Kooboo.CMS.Account.Models.Permission.Sites_Page_EditPermission);

    var hasDraft = ServiceFactory.PageManager.HasDraft(Model.Page);
    var isABTestPage = new Kooboo.CMS.Sites.ABTest.ABPageSetting() { Site = Kooboo.CMS.Sites.Models.Site.Current, MainPage = Model.Page.FullName }.AsActual() != null;

    var routeValues = ViewContext.RequestContext.AllRouteValues()
.Merge("controller", "Page")
.Merge("UUID", Model.Page.FullName)
.Merge("return", Request.RawUrl);

    <div class="map-item @( isLocal ? "" : "inherited") @(isStaticPage ? "static" : "dynamic") @(!Model.Page.Published == true ? "unpublished" : "") @(Model.Page.Navigation.Show ? "show-in-menu" : "") @(isABTestPage == true ? "ab-testing" : "")">
        <span class="status">@Html.IconImage("status")</span>
        <span class="text trigger" title="@(Model.Page.Name)">
            <span>@((Model.Page.Navigation == null || string.IsNullOrEmpty(Model.Page.Navigation.DisplayText)) ? Model.Page.Name : Model.Page.Navigation.DisplayText)</span>
            @Html.IconImage("arrow white-right")
        </span>
        @if (isLocal)
        {
            <ul>
                @if (isStaticPage)
                {
                    <li>
                        <a href="@FrontUrlHelper.Preview(Url, Kooboo.CMS.Sites.Models.Site.Current, Model.Page, null)" target="_blank">
                            @("Preview".Localize())
                        </a>
                    </li>
                }
                @if (hasEditPagePermission)
                {
                    <li class="@(hasDraft == true ? "has-sub" : "")">
                        @if (!hasDraft)
                        {
                            @Html.ActionLink("Edit".Localize(), "Edit", routeValues)
                        }
                        else
                        {
                            <a>
                                @("Edit".Localize())
                            </a>
                            <ul>
                                <li>
                                    @Html.ActionLink("Edit".Localize(), "Edit", routeValues)
                                </li>
                                <li>
                                    @Html.ActionLink("Draft".Localize(), "Draft", routeValues)
                                </li>
                            </ul>
                        }
                    </li>

                    <li>
                        @Html.ActionLink("Copy".Localize(), "Copy", routeValues)
                    </li>

                    <li class="has-sub">
                        <a href="#">
                            @("New sub page".Localize())
                        </a>
                        @Html.Partial("_LayoutList", ViewBag.IsRoot == true ? new RouteValueDictionary(routeValues) : new RouteValueDictionary(routeValues).Merge("parentPage", Model.Page.FullName))
                    </li>
                    if (allowToPublish)
                    {
                        if (!Model.Page.HasParentVersion())
                        {
                            <li>
                                @Html.ActionLink("Delete".Localize(), "Delete1", "Page", routeValues, new RouteValueDictionary { { "data-ajax", "POST" },
                           { "data-confirm",Model.Page.AsActual().IsDefault ? "This page is set as homepage. Are you sure you want to remove it?".Localize(): "Are you sure you want to delete this page?".Localize() } })
                            </li>
                        }
                        else
                        {
                            <li>
                                @Html.ActionLink("Unlocalize".Localize(), "Unlocalize", routeValues, new RouteValueDictionary { { "data-ajax", "POST" }, { "data-confirm", "Are you sure you want to delete this page?".Localize() } })
                            </li>
                        }

                        if (!Model.Page.Published.Value)
                        {
                            <li>
                                @Html.ActionLink("Publish".Localize(), "Publish", routeValues, new RouteValueDictionary { { "data-ajax", "POST" }, { "data-confirm", "Are you sure you want to publish this page?".Localize() } })
                            </li>
                        }
                        else
                        {
                            <li>
                                @Html.ActionLink("Unpublish".Localize(), "Unpublish", routeValues, new RouteValueDictionary { { "data-ajax", "POST" }, 
                           { "data-confirm", Model.Page.AsActual().IsDefault ? "This page is set as homepage. Are you sure you want to remove it?".Localize():"Are you sure you want to unpublish this page?".Localize() } })
                            </li>
                        }

                        <li>
                            @Html.ActionLink("Move To".Localize(), "Move", routeValues, new RouteValueDictionary())
                        </li>
                    }
                }
            </ul>
        }
        else
        {
            <ul>
                @if (isStaticPage)
                {
                    <li>
                        <a href="@FrontUrlHelper.Preview(Url, Kooboo.CMS.Sites.Models.Site.Current, Model.Page, null)"
                           title="preview" target="_blank">
                            @("Preview".Localize())
                        </a>
                    </li>
                }
                <li>
                    <a href="@this.Url.Action("Localize1", "Page", routeValues) " data-confirm="@("Are you sure you want to localize this item?".Localize())" data-ajax="POST">
                        @("Localize".Localize())
                    </a>
                </li>
            </ul>
        }
    </div>

    <div class="children">
        <ul>
            @foreach (var item in Model.Children)
            {
                ViewBag.IsRoot = false;
                <li class="tree-node clearfix active">
                    @if (item.Children.Count() > 0)
                    {
                        @Html.IconImage("tree-icon")
                    }
                    @Html.Partial("SiteMapNode", item)
                </li>
            }
            @foreach (var unsyncedPage in ServiceFactory.PageManager.GetUnsyncedSubPages(Kooboo.CMS.Sites.Models.Site.Current, Model.Page.FullName))
            {
                <li class="tree-node active">
                    @Html.Partial("UnsyncedPageNode", unsyncedPage)
                </li>
            }
        </ul>
    </div>

} 